package net.zxjava.gateway.filters;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

public class ErrorFilter extends ZuulFilter {

	@Override
	public boolean shouldFilter() {
		return true;
	}

	@Override
	public Object run() {
		RequestContext ctx = RequestContext.getCurrentContext();

		HttpServletRequest request = ctx.getRequest();

		String resUrl = request.getServletPath();
		System.out.println(resUrl);
		if (ctx.getResponseStatusCode() != 200) {
			String responseApi = null;
			if (ctx.getResponseStatusCode() == 404) {
				responseApi = "404";
			}

			if (ctx.getResponseStatusCode() == 400) {
				responseApi = "error";
			}

			if (responseApi == null) {
				responseApi = "system error";
			}
			ctx.setSendZuulResponse(false);
			ctx.setResponseStatusCode(200);
			ctx.setResponseBody(JSON.toJSONString(responseApi));
		}
		return null;
	}

	@Override
	public String filterType() {
		return "post";
	}

	@Override
	public int filterOrder() {
		return 0;
	}

}
